<?php
require_once (APPLICATION_DIR."/library/external/XOAD/xoad.php");
require_once(APPLICATION_DIR."/library/external/fckeditor/fckeditor.php") ;
require_once (APPLICATION_DIR."/library/IO/IO_Image.php");

class AdminController extends DRX_Controller 
{
	function xoadGetMeta()
	{
		//register function can be called by JS
	    XOAD_Client::mapMethods($this,array("editSubmenuPage","saveSubmenu","deleteSubmenu","addNews","editNews","deleteNews","loadAdmissionContent","editProfilePage","deleteProfile","toggleNewsPublish","deleteGroup","editGroup","saveGroup","addGroup","editGroupQuestion","addQuestionToGroup","deleteGroupQuestion","saveGroupQuestionOrder","toggleGroupHide","editFaq","saveFaq","deleteFaq","addFaq","answerQuestion","saveQuestion","deleteQuestion","copyQuestion","deleteProgramme","addPartnerAdmission","editLink","saveLink","addPartner","editPartner","savePartner","deletePartner","deleteAdmissionSubpage", "frmaddlink", "doaddlink"));
	}
	
	function doaddlink($p_arrArgs)
	{
		$arrForm = $p_arrArgs['form'];
		$link = new Link();
		$link->addLink($arrForm);
		$this->refreshLinkList();
		return "OK";
	}
	
	function frmaddlink()
	{
		$smarty       = DRX::load("smarty");
		$content = $smarty->fetch("admin/link/add_link.tpl");
		$div = XOAD_HTML::getElementById('edit_link_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function alumniAction()
	{
		global $arrArgs;
		
		define("SIZE_THUMBNAIL_NEWS", 270);
		define("SIZE_THUMBNAIL_SMALL", 86);
		
		if ( !isset( $arrArgs[0] ) || trim($arrArgs[0]) == "" )
			$arrArgs[0] = "news";
		if ( !isset( $arrArgs[1] ) || trim($arrArgs[1]) == "" )
			$arrArgs[1] = "index";
		
		$ControllerName = "Adminalumni" . ($arrArgs[0]) . "Controller";
		$ActionName 	= $arrArgs[1]."Action";
		
		if(file_exists("application/controller/".$ControllerName.".php")) 
		{
			if (class_exists($ControllerName)) 
			{
				$objHandlerController = new $ControllerName();
				if(method_exists($objHandlerController,$ActionName)) 
				{
					$objHandlerController->$ActionName();
				}
				else 
				{
					die("Cannot find method <strong>".$ActionName."</strong> in ".$ControllerName." class to execute");
				}
			}
			else
			{
				die("Cannot find className <strong>".$ControllerName."</strong> in ".$ControllerName.".php modules to execute");
			}		
		}
		else
		{
			die("Cannot find controller modules $ControllerName to execute");
		}
	}
	
	
	function indexAction()
	{	
		$smarty 	= DRX::load("smarty");
  		
//		$account['type']		= "admin";
//		session_start();
//		$_SESSION['account'] 	= $account;
		
		$smarty->assign("page_title",ADMIN_HOMEPAGE_TITLE);	
		$this->preparePathWeb();
		$content	= $smarty->fetch("admin/home.tpl");
		$smarty->assign("content",$content);
		
		$smarty->display("admin/admin.tpl");
	}
	
	function prepareLeftPane($top_index)
	{
	    $smarty 	= DRX::load("smarty");
	    $leftmenu 	= $smarty->fetch("admin/leftpane/leftpane$top_index.tpl");
		$smarty->assign("leftmenu",$leftmenu);
	}
	
	function listOfTopMenu()
    {
        $top[ADMIN_TOPMENU_ABOUT]['title']      = ADMIN_TOPMENU_ABOUT_TITLE;
        $top[ADMIN_TOPMENU_PROGRAMME]['title']  = ADMIN_TOPMENU_PROGRAMME_TITLE;
        $top[ADMIN_TOPMENU_ADMISSION]['title']  = ADMIN_TOPMENU_ADMISSION_TITLE;
        $top[ADMIN_TOPMENU_PARTNER]['title']    = ADMIN_TOPMENU_PARTNER_TITLE;
        $top[ADMIN_TOPMENU_INSTRUCTOR]['title'] = ADMIN_TOPMENU_INSTRUCTOR_TITLE;
        $top[ADMIN_TOPMENU_NEWS]['title']       = ADMIN_TOPMENU_NEWS_TITLE;
        $top[ADMIN_TOPMENU_STUDENT]['title']    = ADMIN_TOPMENU_STUDENT_TITLE;
        $top[ADMIN_TOPMENU_ALUMNI]['title']     = ADMIN_TOPMENU_ALUMNI_TITLE;
        $top[ADMIN_TOPMENU_FAQ]['title']        = ADMIN_TOPMENU_FAQ_TITLE;
        $top[ADMIN_TOPMENU_CONTACT]['title']    = ADMIN_TOPMENU_CONTACT_TITLE;
        
        return $top;
    }
    
    function listOfSubMenu()
    {
        $sub[ADMIN_TOPMENU_ABOUT][ADMIN_SUBMENU_ABOUT_HOMEPAGE_INDEX]['title']      = ADMIN_SUBMENU_ABOUT_HOMEPAGE_TITLE;
        $sub[ADMIN_TOPMENU_ABOUT][ADMIN_SUBMENU_ABOUT_HOMEPAGE_INDEX]['action']     = ADMIN_SUBMENU_ABOUT_HOMEPAGE_ACTION;
        $sub[ADMIN_TOPMENU_ABOUT][ADMIN_SUBMENU_ABOUT_SUBPAGE_INDEX]['title']       = ADMIN_SUBMENU_ABOUT_SUBPAGE_TITLE;
        $sub[ADMIN_TOPMENU_ABOUT][ADMIN_SUBMENU_ABOUT_SUBPAGE_INDEX]['action']      = ADMIN_SUBMENU_ABOUT_SUBPAGE_ACTION;
        
        $sub[ADMIN_TOPMENU_PROGRAMME][ADMIN_SUBMENU_PROGRAMME_HOMEPAGE_INDEX]['title']  = ADMIN_SUBMENU_PROGRAMME_HOMEPAGE_TITLE;
        $sub[ADMIN_TOPMENU_PROGRAMME][ADMIN_SUBMENU_PROGRAMME_HOMEPAGE_INDEX]['action'] = ADMIN_SUBMENU_PROGRAMME_HOMEPAGE_ACTION;
        $sub[ADMIN_TOPMENU_PROGRAMME][ADMIN_SUBMENU_PROGRAMME_SUBPAGE_INDEX]['title']   = ADMIN_SUBMENU_PROGRAMME_SUBPAGE_TITLE;
        $sub[ADMIN_TOPMENU_PROGRAMME][ADMIN_SUBMENU_PROGRAMME_SUBPAGE_INDEX]['action']  = ADMIN_SUBMENU_PROGRAMME_SUBPAGE_ACTION;
        
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_HOMEPAGE_INDEX]['title']  = ADMIN_SUBMENU_ADMISSION_HOMEPAGE_TITLE;
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_HOMEPAGE_INDEX]['action'] = ADMIN_SUBMENU_ADMISSION_HOMEPAGE_ACTION;
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_SUBPAGE_INDEX]['title']   = ADMIN_SUBMENU_ADMISSION_SUBPAGE_TITLE;
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_SUBPAGE_INDEX]['action']  = ADMIN_SUBMENU_ADMISSION_SUBPAGE_ACTION;
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_PARTNER_INDEX]['title']   = ADMIN_SUBMENU_ADMISSION_PARTNER_TITLE;
        $sub[ADMIN_TOPMENU_ADMISSION][ADMIN_SUBMENU_ADMISSION_PARTNER_INDEX]['action']  = ADMIN_SUBMENU_ADMISSION_PARTNER_ACTION;
        
        
        $sub[ADMIN_TOPMENU_INSTRUCTOR][ADMIN_SUBMENU_INSTRUCTOR_BANNER_INDEX]['title']  = ADMIN_SUBMENU_INSTRUCTOR_BANNER_TITLE;
        $sub[ADMIN_TOPMENU_INSTRUCTOR][ADMIN_SUBMENU_INSTRUCTOR_BANNER_INDEX]['action'] = ADMIN_SUBMENU_INSTRUCTOR_BANNER_ACTION;
        $sub[ADMIN_TOPMENU_INSTRUCTOR][ADMIN_SUBMENU_INSTRUCTOR_PROFILE_INDEX]['title']  = ADMIN_SUBMENU_INSTRUCTOR_PROFILE_TITLE;
        $sub[ADMIN_TOPMENU_INSTRUCTOR][ADMIN_SUBMENU_INSTRUCTOR_PROFILE_INDEX]['action'] = ADMIN_SUBMENU_INSTRUCTOR_PROFILE_ACTION;
        
        $sub[ADMIN_TOPMENU_STUDENT][ADMIN_SUBMENU_STUDENT_ELEARNING_INDEX]['title']     = ADMIN_SUBMENU_STUDENT_ELEARNING_TITLE;
        $sub[ADMIN_TOPMENU_STUDENT][ADMIN_SUBMENU_STUDENT_ELEARNING_INDEX]['action']    = ADMIN_SUBMENU_STUDENT_ELEARNING_ACTION;
        $sub[ADMIN_TOPMENU_STUDENT][ADMIN_SUBMENU_STUDENT_ALUMNI_INDEX]['title']        = ADMIN_SUBMENU_STUDENT_ALUMNI_TITLE;
        $sub[ADMIN_TOPMENU_STUDENT][ADMIN_SUBMENU_STUDENT_ALUMNI_INDEX]['action']       = ADMIN_SUBMENU_STUDENT_ALUMNI_ACTION;
        
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_HOMEPAGE_INDEX]['title']    = ADMIN_SUBMENU_ALUMNI_HOMEPAGE_TITLE;
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_HOMEPAGE_INDEX]['action']   = ADMIN_SUBMENU_ALUMNI_HOMEPAGE_ACTION;
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_SUCCESS_INDEX]['title']     = ADMIN_SUBMENU_ALUMNI_SUCCESS_TITLE;
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_SUCCESS_INDEX]['action']    = ADMIN_SUBMENU_ALUMNI_SUCCESS_ACTION;
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_TESTIMONIAL_INDEX]['title'] = ADMIN_SUBMENU_ALUMNI_TESTIMONIAL_TITLE;
        $sub[ADMIN_TOPMENU_ALUMNI][ADMIN_SUBMENU_ALUMNI_TESTIMONIAL_INDEX]['action']= ADMIN_SUBMENU_ALUMNI_TESTIMONIAL_ACTION;
        
        $sub[ADMIN_TOPMENU_NEWS][ADMIN_SUBMENU_NEWS_BANNER_INDEX]['title']      = ADMIN_SUBMENU_NEWS_BANNER_TITLE;
        $sub[ADMIN_TOPMENU_NEWS][ADMIN_SUBMENU_NEWS_BANNER_INDEX]['action']     = ADMIN_SUBMENU_NEWS_BANNER_ACTION;
        $sub[ADMIN_TOPMENU_NEWS][ADMIN_SUBMENU_NEWS_EDIT_NEWS_INDEX]['title']   = ADMIN_SUBMENU_NEWS_EDIT_NEWS_TITLE;
        $sub[ADMIN_TOPMENU_NEWS][ADMIN_SUBMENU_NEWS_EDIT_NEWS_INDEX]['action']  = ADMIN_SUBMENU_NEWS_EDIT_NEWS_ACTION;

        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_BANNER_INDEX]['title']        = ADMIN_SUBMENU_FAQ_BANNER_TITLE;
        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_BANNER_INDEX]['action']       = ADMIN_SUBMENU_FAQ_BANNER_ACTION;
        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_EDIT_GROUP_INDEX]['title']    = ADMIN_SUBMENU_FAQ_EDIT_GROUP_TITLE;
        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_EDIT_GROUP_INDEX]['action']   = ADMIN_SUBMENU_FAQ_EDIT_GROUP_ACTION;
        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_EDIT_FAQ_INDEX]['title']      = ADMIN_SUBMENU_FAQ_EDIT_FAQ_TITLE;
        $sub[ADMIN_TOPMENU_FAQ][ADMIN_SUBMENU_FAQ_EDIT_FAQ_INDEX]['action']     = ADMIN_SUBMENU_FAQ_EDIT_FAQ_ACTION;
        
        $sub[ADMIN_TOPMENU_CONTACT][ADMIN_SUBMENU_CONTACT_HOMEPAGE_INDEX]['title']    = ADMIN_SUBMENU_CONTACT_HOMEPAGE_TITLE;
        $sub[ADMIN_TOPMENU_CONTACT][ADMIN_SUBMENU_CONTACT_HOMEPAGE_INDEX]['action']   = ADMIN_SUBMENU_CONTACT_HOMEPAGE_ACTION;        
        
        return $sub;
    }
    
    function assignSubmenuPathWeb($top_index, $sub_index)
    {        
        $prefix_url             = APPLICATION_URL . ADMIN_URL;        
        $list_path[0]['url']    = $prefix_url;
        $list_path[0]['title']  = ADMIN_HOME_PATH_WEB_TITLE;
        $topmenu = $this->listOfTopMenu();
        if ($sub_index==0)
        {
            if ($top_index == ADMIN_TOPMENU_ALUMNI)
            {
                $list_path[1]['url']    = $prefix_url . ADMIN_ACTION_TOP . ADMIN_TOPMENU_STUDENT;
                $list_path[1]['title']  = ADMIN_TOPMENU_STUDENT_TITLE;
            }
            $last_path['title'] = $topmenu[$top_index]['title'];
            $last_path['url']   = APPLICATION_URL . ADMIN_URL . ADMIN_ACTION_TOP . $top_index;
        }
        else 
        {            
            if ($top_index != ADMIN_TOPMENU_ALUMNI)
            {
                $list_path[1]['url']    = $prefix_url . ADMIN_ACTION_TOP . $top_index;
                $list_path[1]['title']  = $topmenu[$top_index]['title'];
            }
            else 
            {
                $list_path[1]['url']    = $prefix_url . ADMIN_ACTION_TOP . ADMIN_TOPMENU_STUDENT;
                $list_path[1]['title']  = ADMIN_TOPMENU_STUDENT_TITLE;
                $list_path[2]['url']    = $prefix_url . ADMIN_ACTION_TOP . $top_index;
                $list_path[2]['title']  = $topmenu[$top_index]['title'];
            }
            $sub = $this->listOfSubMenu();
            $last_path['title'] = $sub[$top_index][$sub_index]['title'];
            $last_path['url']   = $prefix_url . $sub[$top_index][$sub_index]['action'] . $top_index . "/" . $sub_index;
        }
        $smarty 	= DRX::load("smarty");
        $smarty->assign("list_path",$list_path);
        $smarty->assign("last_path",$last_path);
    }
	
	function preparePathWeb($top_index = 0,$sub_index = 0)
	{
	    $smarty 	= DRX::load("smarty");        
        if ($top_index)
        {
            $this->assignSubmenuPathWeb($top_index,$sub_index);            
        }
        else 
        {   //Homepage
            $last_path['title'] = ADMIN_HOME_PATH_WEB_TITLE;
            $last_path['url']   = APPLICATION_URL . ADMIN_URL;
            $smarty->assign("last_path",$last_path);
        }
		$path_web   = $smarty->fetch("admin/path_web.tpl");
	    $smarty->assign("path_web",$path_web);
	}
	
	function prepareLayout($top_index = 0,$sub_index = 0)
	{
	    $this->preparePathWeb($top_index,$sub_index);
	    $this->prepareLeftPane($top_index);
	}
	
	function topAction()
	{
		global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];
		
		if ($top_index>0 && $top_index<ADMIN_TOPMENU_COUNT)
		{
			$this->prepareLayout($top_index);
			
			$smarty 	= DRX::load("smarty");	
			$help_page	= "admin/help/help_page" . "$top_index.tpl";
			$content	= $smarty->fetch($help_page);
			$smarty->assign("content",$content);
			
			$smarty->display("admin/admin.tpl");
		}
		else 
		{
			redirect(APPLICATION_URL . ADMIN_URL);
		}
	}
	
	function createFCK($fck_value = "")
	{
		$fck_name = FCK_DEFAULT_NAME;
		if (!isset($fck_value))
		{
			$fck_value=FCK_DEFAULT_TEXT_EDITOR_VALUE;
		}
		
		$oFCKeditor = new FCKeditor($fck_name) ;
		$oFCKeditor->BasePath = FCK_BASE_PATH;
		$oFCKeditor->Height = FCK_DEFAULT_HEIGHT;
		$oFCKeditor->Value = $fck_value;
		$fck = $oFCKeditor->Create();
		return $fck;
	}
	
	function toppageAction()
	{	
		global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];		
		$return_url = APPLICATION_URL . "admin/top/$top_index";	
		
		$this->prepareLayout($top_index,$sub_index);
		
		switch ($top_index)
		{
		    case ADMIN_TOPMENU_ALUMNI:
		        switch ($sub_index)
        	    {
        	        case ADMIN_SUBMENU_EDIT_ALUMNI_HOME:
        	            $page_edit= "Alumni";
				        $node_id  = ALUMNI_ID;
        	            break;
        	            
        	        case ADMIN_SUBMENU_EDIT_TESTIMONIAL:
        	            $page_edit= "Testimonial";
				        $node_id  = TESTIMONIAL_ID;
				        break;   

    	            default:
        	            redirect($return_url);
        	    }
		        break;
		    
			case ADMIN_TOPMENU_ABOUT:
			    $page_edit= "About Us";
				$node_id  = ABOUT_ID;
				break;
			
			case ADMIN_TOPMENU_PROGRAMME:
				$page_edit= "Programme";
				$node_id  = PROGRAMME_ID;
				break;
			
			case ADMIN_TOPMENU_ADMISSION:
			    $page_edit= "Admission";
			    $node_id  = ADMISSION_ID;
			    break;	
			
			case ADMIN_TOPMENU_PARTNER:
			    $page_edit= "Partner";
			    $node_id  = PARTNER_ID;
				break;
				
			case ADMIN_TOPMENU_TOOL:
			    $page_edit = "Tool";
			    $node_id = TOOL_ID;
			    break;
			    		
			case ADMIN_TOPMENU_CONTACT:
			    $page_edit = "Contact";
			    $node_id = CONTACT_ID;
			    break;
			    
			default:
				redirect(APPLICATION_URL . "admin");
		}
		
		$smarty 	= DRX::load("smarty");
		$smarty->assign("return_url",$return_url);
		
		$node 		= new Node();
		$nodeInfo = $node->getNodeInfo($node_id);
		$nodeInfo->fetch();
		$node_title	  = $nodeInfo->node_title;
		$node_content = $nodeInfo->node_content;
		$smarty->assign("page_edit",$page_edit);
		$smarty->assign("node_title",$node_title);
		$smarty->assign("node_id",$node_id);
		$fck = $this->createFCK($node_content);
        $smarty->assign("fck_editor",$fck);
        
        $topmenu    = new Topmenu();
        $topmenu->prepareTopBanner($node_id);
        $content = $smarty->fetch("admin/toppage/edit_page.tpl");
        $smarty->assign("content",$content);
		$smarty->display("admin/admin.tpl");
	}
	
	function subpageAction()
	{	
		$smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
		global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];		
		$current_url = APPLICATION_URL . "admin/subpage/$top_index/$sub_index";	
		
		$this->prepareLayout($top_index,$sub_index);
		
		switch ($top_index)
		{
			case ADMIN_TOPMENU_ABOUT:
				$topmenu_id = ABOUT_ID;
				break;
			
			case ADMIN_TOPMENU_PROGRAMME:
				$topmenu_id = PROGRAMME_ID;
				break;
				
			case ADMIN_TOPMENU_PARTNER:
			    $topmenu_id = PARTNER_ID;
			    break;
			    
			case ADMIN_TOPMENU_TOOL:
			    $topmenu_id = TOOL_ID;
				break;
					
			default:
				redirect(APPLICATION_URL . "admin");
		}
		
		$submenu = new Submenu();
		$submenu_list = $submenu->getSubMenuTitle($topmenu_id);
		if ($submenu_list)
		{
			$hasSubpage = true;
		}
		
		$smarty->assign("current_url",$current_url);
//		$smarty->assign("page_message",$page_message);
		$smarty->assign("hasSubpage",$hasSubpage);
		$smarty->assign("submenu_list",$submenu_list);
		$content = $smarty->fetch("admin/submenu/subpage_list.tpl");
		$smarty->assign("content",$content);
		$smarty->display("admin/admin.tpl");
	}
	
	function editSubmenuPage($args)
	{
		$node_id 	= $args["node_id"];
		$menu_title = $args["menu_title"];
		
		$node 		= new Node();
		$nodeInfo	= $node->getNodeInfo($node_id);
		$nodeInfo->fetch();
		$node_title	= $nodeInfo->node_title;
		$fck_value  = $nodeInfo->node_content;
			
		$fck		= $this->createFCK($fck_value);
			
		$smarty 	= DRX::load("smarty");
		$smarty->assign("node_id",$node_id);
		$smarty->assign("menu_title",$menu_title);
		$smarty->assign("fck_editor",$fck);
		$smarty->assign("node_title",$node_title);
		
		$content = $smarty->fetch("admin/submenu/edit_subpage.tpl");
		
		$div = XOAD_HTML::getElementById('edit_page_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function savepageAction()
	{
		$node_id      = $_REQUEST['node_id'];
		$node_title   = $_REQUEST['node_title'];
		$return_url   = $_REQUEST['return_url'];
		if ($_FILES['menu_banner']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['menu_banner']['tmp_name'];
            $filename = $_FILES['menu_banner']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = TOPMENU_BANNER_DIR . "top$node_id." . $file_ext;
            
            if (!move_uploaded_file($_FILES['menu_banner']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $menu_banner = "";
            }
            else
            {
                $menu_banner = $server_copy;
                chmod("$server_copy",0777);
            }
  		}
		$node_content = stripslashes($_REQUEST['fck_editor']);
		$node = new Node();
		$node->updateNode($node_id,$node_title,$node_content);
		
		if ($menu_banner)
		{
		    $topmenu = new Topmenu();
		    $topmenu->updateBanner($node_id,$menu_banner);
		}
		
		redirect($return_url);
	}
	
	function submenuAction()
	{  		
		$smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
		
  		global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];
		$current_url = APPLICATION_URL . "admin/submenu/$top_index/$sub_index";
		$smarty->assign("current_url",$current_url);
		
		
		if ($top_index>0 && $top_index<ADMIN_TOPMENU_COUNT)
		{
			$this->prepareLayout($top_index,$sub_index);
			
			switch ($top_index)
			{
				case ADMIN_TOPMENU_ABOUT: 
					$topmenu_id = ABOUT_ID;
					$page_message = PAGE_MESSAGE_MENU_LIST_ABOUT;
					break;
				
				case ADMIN_TOPMENU_PROGRAMME:
				    $topmenu_id = PROGRAMME_ID;
				    $page_message = PAGE_MESSAGE_MENU_LIST_PROGRAMME;
				    break;

				case ADMIN_TOPMENU_ADMISSION:
				    $topmenu_id = PARTNER_ID;
				    $page_message = PAGE_MESSAGE_MENU_LIST_PARTNER;
				    break;

				case ADMIN_TOPMENU_TOOL:
				    $topmenu_id = TOOL_ID;
				    $page_message = PAGE_MESSAGE_MENU_LIST_TOOL;
				    break;
				    
				default:
					redirect(APPLICATION_URL . "admin");
			}
			$submenu = new Submenu();
			$submenu_list = $submenu->getSubMenuTitle($topmenu_id);
			if ($submenu_list)
			{
			    $hasSubmenu = true;
			}
		    $menu_parent_id = $topmenu_id;
		    $smarty->assign("page_message",$page_message);
		    $smarty->assign("hasSubmenu",$hasSubmenu);
			$smarty->assign("submenu_list",$submenu_list);
			$smarty->assign("menu_parent_id",$menu_parent_id);
			
			$content 	= $smarty->fetch("admin/submenu/submenu.tpl");
			$smarty->assign("content",$content);
			
			$smarty->display("admin/admin.tpl");
		}
		else 
		{
			redirect(APPLICATION_URL . "admin");
		}
	}
	
	function saveSubmenu($args)
	{
		$form = $args['form'];
		if ($form['menu_id'])
		{
			//edit
			$submenu	= new Submenu();
			$submenu->updateSubmenu($form);			
		}
		else 
		{
			//add new
			$submenu	= new Submenu();
			$submenu->addSubmenu($form);		
		}
		$this->refreshSubmenuList($form['menu_parent_id']);
		return "OK";
	}
	
	function refreshSubmenuList($parent_id)
	{
		$smarty 	= DRX::load("smarty");
		$submenu = new Submenu();
		$submenu_list = $submenu->getSubMenuTitle($parent_id);
		if ($submenu_list)
		{
		    $hasSubmenu = true;
		}
		$smarty->assign("hasSubmenu",$hasSubmenu);		
		$smarty->assign("submenu_list",$submenu_list);
		$html 	= $smarty->fetch("admin/submenu/submenu_list.tpl");
		$div	= XOAD_HTML::getElementById('submenu_list');
		$div->innerHTML = $html;
	}
	
	function deleteSubmenu($args)
	{
		$menu_id 		= $args['menu_id'];
		$menu_parent_id = $args['menu_parent_id'];
		
		if ($menu_parent_id == PROGRAMME_ID)
		{
		    //also remove programme admission
		    $programme = new Programme();
		    $programme->deleteAdmission($menu_id);
		}
		
		$submenu = new Submenu();
		$result = $submenu->deleteSubmenu($menu_id);
		if ($result)
		{
			$this->refreshSubmenuList($menu_parent_id);
			return "OK";
		}
		else 
		{
			return "Error";
		}
	}
	
	function get_admission_file($file_location)
	{
	    $list = explode("/",$file_location);
	    $filedir  = $list[count($list)-2];
	    $filename = $list[count($list)-1];
	    $url = ADMISSION_FORM_URL . $filename;
	    return $url;
	}
	
	function loadAdmissionContent($args)
	{
        $smarty = DRX::load('smarty');
      
        $menu_id    = $args['menu_id'];
        $type       = $args['admission'];
        $admission_title = $args['admission_title'];
        $partner_id = $args['partner_id'];
        $programme  = new Programme();
        $proInfo    = $programme->getAdmissionInfo($menu_id,$type,$partner_id);
        if ($proInfo->fetch())
        {
            $pro_id         = $proInfo->pro_id;
            $pro_title      = $proInfo->pro_title;
            $partner_title  = $proInfo->partner_title;
            $pro_content= $proInfo->pro_content;
            if ($type != PROGRAMME_APPLYFORM_INDEX) 
            {
                $fck = $this->createFCK($pro_content);
            }
            else 
            {
                $file_link = $this->get_admission_file($pro_content);
                $list = explode("/",$file_link);
        	    $file_name = $list[count($list)-1];
                $smarty->assign("file_link",$file_link);
                $smarty->assign("file_name",$file_name);
                $fck = $smarty->fetch("admin/admission/apply_form.tpl");
            }
        }
        else 
        {
            return "Unsual error.";
        }
        
		$return_url = APPLICATION_URL . "admin/admission/" . ADMIN_TOPMENU_PROGRAMME . "/4";
		$smarty->assign("return_url",$return_url);
        
        $smarty->assign("pro_title",$pro_title);
        $smarty->assign("partner_title",$partner_title);
        $smarty->assign("admission_title",$admission_title);
        $smarty->assign("pro_id",$pro_id);
        $smarty->assign("pro_type",$type);
        $smarty->assign("fck_editor",$fck);
        
        $edit_admission = $smarty->fetch("admin/admission/edit_admission.tpl");
		XOAD_HTML::getElementById('edit_admission_section') -> innerHTML = $edit_admission;
		return "OK";
	}
	
	function saveadmissionAction()
	{
	    $return_url = $_REQUEST['return_url'];
	    $pro_id = $_REQUEST['pro_id'];
		$pro_type = $_REQUEST['pro_type'];
		if ($pro_type != PROGRAMME_APPLYFORM_INDEX) 
		{
		    $pro_content = stripslashes($_REQUEST['fck_editor']);
		}
		else 
		{
		    if ($_FILES['apply_form']['error'] == UPLOAD_ERR_OK)
            {
                $tmp_name = $_FILES['apply_form']['tmp_name'];
                $filename = $_FILES['apply_form']['name'];
                $filename = str_replace(" ","_",$filename);
                $server_copy = ADMISSION_FORM_DIR . "programme$pro_id" . "_$filename";
                if (!move_uploaded_file($_FILES['apply_form']['tmp_name'],$server_copy))
                {
                    $result = "Error while uploading file";
                    $pro_content = "";
                }
                else
                {
                    $pro_content = $server_copy;
                    chmod("$server_copy",0777);
                }
      		}
		}
		$pro = new Programme();
    	$pro->updateAdmissionInfo($pro_id,$pro_content,$pro_type);
    	redirect($return_url);
	}
	
	function deleteProgramme($args)
	{
	    $pro_id = $args['pro_id'];
	    $programme = new Programme();
	    $programme->deleteProgramme($pro_id);
	    $this->refreshAdmissionList();	    
	    return "OK";
	}
	
	function refreshAdmissionList()
	{
	    $smarty 	= DRX::load("smarty");
	    $programme   = new Programme();
	    $partner_admission = $programme->getAllPartnerAdmission();
        $smarty->assign("partner_admission",$partner_admission);
        $content = $smarty->fetch("admin/admission/programme_list.tpl");
		$div	= XOAD_HTML::getElementById('programme_list');
		$div->innerHTML = $content;
	}
	
	function addPartnerAdmission($args)
	{
	    $smarty = DRX::load('smarty');
        $menu_id    = $args['menu_id'];
        $partner_id = $args['partner_id'];
        $programme  = new Programme();
        $result = $programme->addProgramme($menu_id,$partner_id);
        if ($result)
        {
            return $result;
        }
        else
        {
            $this->refreshAdmissionList();
            return "OK";
        }
	}
	
	function admissionAction()
	{
	    global $arrArgs;
	    $top_index = $arrArgs[0];
	    $sub_index = $arrArgs[1];
	    $this->prepareLayout($top_index,$sub_index);
	    
        $smarty = DRX::load('smarty');
	    //ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
	    $submenu   = new Submenu();
	    $pro_list = $submenu->getSubMenuTitle(PROGRAMME_ID);
	    if ($pro_list)
	    {
	        $hasProgramme = true;
	    }
	    $partner = new Partner();
	    $partnerInfo = $partner->getAllPartner();
	    $partner_list = $partnerInfo->fetchList();
	    if (count($partner_list))
	    {
	        $hasPartner = true;
	    }
	    $programme   = new Programme();
	    $partner_admission = $programme->getAllPartnerAdmission();

	    $smarty->assign("partner_admission",$partner_admission);    
	    $smarty->assign("pro_list",$pro_list);
	    $smarty->assign("partner_list",$partner_list);
	    $smarty->assign("hasProgramme",$hasProgramme);
	    $smarty->assign("hasPartner",$hasPartner);
	    
	    $content = $smarty->fetch("admin/admission/admission.tpl");
	    $smarty->assign("content",$content);
	    $smarty->display("admin/admin.tpl");
	}
	
	function addPartner($args)
	{
	    $smarty = DRX::load('smarty');
	    $smarty->assign("page_message","Add partner:");
	    $fck = $this->createFCK();
	    $smarty->assign("fck_editor",$fck);
	    $content = $smarty->fetch("admin/admission/edit_partner.tpl");
	    
	    $div	= XOAD_HTML::getElementById('edit_partner_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function editPartner($args)
	{
	    $smarty = DRX::load('smarty');
	    $smarty->assign("page_message","Edit partner:");
	    $partner_id = $args['partner_id'];
	    $partner = new Partner();
	    $partnerInfo = $partner->getPartnerByID($partner_id);
	    if ($partnerInfo->fetch())
	    {
    	    $partner_title = $partnerInfo->partner_title;
    	    $partner_order = $partnerInfo->partner_order;
    	    $partner_intro = $partnerInfo->partner_intro;
	    }
        $fck = $this->createFCK($partner_intro);
	    $smarty->assign("partner_id",$partner_id);
	    $smarty->assign("partner_title",$partner_title);
	    $smarty->assign("partner_order",$partner_order);
	    $smarty->assign("fck_editor",$fck);
	    $content = $smarty->fetch("admin/admission/edit_partner.tpl");
	    
	    $div	= XOAD_HTML::getElementById('edit_partner_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function savePartnerAction()
	{	    
	    $partner_id    = $_REQUEST['partner_id'];
	    $partner_title = $_REQUEST['partner_title'];
	    $partner_order = $_REQUEST['partner_order'];
	    $partner_intro = stripslashes($_REQUEST['fck_editor']);
	    $partner = new Partner();
	    if ($partner_id)
	    {
	        $partner->updatePartner($partner_id,$partner_title,$partner_order, $partner_intro);
	    }
	    else 
	    {
	        $partner->addPartner($partner_title,$partner_order, $partner_intro);
	    }
	    redirect(APPLICATION_URL . "admin/admission/3/2");
	}
	
	function deletePartner($args)
	{
	    $partner_id = $args['partner_id'];
	    $partner = new Partner();
	    $result = $partner->deletePartner($partner_id);
	    $this->refreshPartnerList();
	    return $result;
	}
	
	function deleteAdmissionSubpage($args)
	{
	    $pro_id = $args['pro_id'];
	    $type = $args['type'];
	    $pro = new Programme();
	    $pro->updateAdmissionInfo($pro_id,"",$type);
	    $this->refreshAdmissionList();
	    return "OK";
	}
	
	function refreshPartnerList()
	{
        $smarty = DRX::load('smarty');
	    $partner   = new Partner();
	    $partnerInfo = $partner->getAllPartner();
	    $partner_list = $partnerInfo->fetchList();
	    if (count($partner_list))
	    {
	        $hasPartner = true;
	    }
	    $smarty->assign("partner_list",$partner_list);
	    $smarty->assign("hasPartner",$hasPartner);
	    $content = $smarty->fetch("admin/admission/partner_list.tpl");
	    $div	= XOAD_HTML::getElementById('partner_list_section');
		$div->innerHTML = $content;
		
	    $submenu   = new Submenu();
	    $pro_list = $submenu->getSubMenuTitle(PROGRAMME_ID);
	    if ($pro_list)
	    {
	        $hasProgramme = true;
	    }
	    
	    $smarty->assign("pro_list",$pro_list);
	    $smarty->assign("partner_list",$partner_list);
	    $smarty->assign("hasProgramme",$hasProgramme);
	    $smarty->assign("hasPartner",$hasPartner);
	    $content = $smarty->fetch("admin/admission/add_admission.tpl");
	    $div	= XOAD_HTML::getElementById('programme_edit_select');
		$div->innerHTML = $content;
	}
	
	function getProfileImageURL($image_location)
	{
	    $list = explode("/",$image_location);
	    $filedir  = $list[count($list)-2];
	    $filename = $list[count($list)-1];
	    $url = PROFILE_IMAGE_URL . "$filedir/$filename";
	    return $url;
	}
	
	function editProfilePage($args)
	{
	    $profile_id 	= $args["profile_id"];
		$profile_type   = $args["profile_type"];
		
		switch ($profile_type)
		{
		    case PROFILE_INSTRUCTOR:
		        $profile = new Profile();
		        break;
		        
		    case PROFILE_SUCCESS:
		        $profile = new Success_profile();
		        break;
		        
		    default:
		        break;
		}
		
		if ($profile_id)
		{
    		$profileInfo      = $profile->getProfileInfo($profile_id);
    		$profileInfo->fetch();
    		$profile_title    = $profileInfo->profile_title;
    		$profile_image1   = $this->getProfileImageURL($profileInfo->profile_image1);
    		$profile_image2   = $this->getProfileImageURL($profileInfo->profile_image2);
    		$profile_image3   = $this->getProfileImageURL($profileInfo->profile_image3);
    		$fck_value        = $profileInfo->profile_content;
    		
    		$fck		= $this->createFCK($fck_value);
    		
    		$smarty 	= DRX::load("smarty");
    		$smarty->assign("page_message",PAGE_MESSAGE_EDIT_PROFILE);
    		$smarty->assign("image1",$profile_image1);
    		$smarty->assign("image2",$profile_image2);
    		$smarty->assign("image3",$profile_image3);
    		$image_list = $smarty->fetch("admin/profile/image_list.tpl");
    		$smarty->assign("profile_id",$profile_id);
    		$smarty->assign("profile_type",$profile_type);
    		$smarty->assign("profile_title",$profile_title);
    		$smarty->assign("fck_editor",$fck);
    		$smarty->assign("image_list",$image_list);
    		
    		$content = $smarty->fetch("admin/profile/edit_profile.tpl");
    		
    		$div = XOAD_HTML::getElementById('edit_profile_section');
    		$div->innerHTML = $content;
    	    return "OK";
		}
		else
		{
		    $fck		= $this->createFCK();
    		
    		$smarty 	= DRX::load("smarty");
    		$smarty->assign("page_message",PAGE_MESSAGE_CREATE_PROFILE);
    		$smarty->assign("profile_type",$profile_type);
    		$smarty->assign("fck_editor",$fck);
    		$smarty->assign("image_list",$image_list);
    		$content = $smarty->fetch("admin/profile/edit_profile.tpl");
    		
    		$div = XOAD_HTML::getElementById('edit_profile_section');
    		$div->innerHTML = $content;
    	    return "OK";
		}
	}
	
	function deleteProfile($args)
	{
	    $profile_id = $args['profile_id'];
		$profile_type = $args['profile_type'];
		switch ($profile_type)
		{
		    case PROFILE_INSTRUCTOR:
		        $profile = new Profile();
		        break;
		        
		    case PROFILE_SUCCESS:
		        $profile = new Success_profile();
		        break;
		        
		    default:
		        return ("Error");   
		}
		$profile->deleteProfile($profile_id);
		$this->refreshProfileList($profile_type);
		return "OK";
	}
	
	function refreshProfileList($profile_type)
	{
	    $smarty 	= DRX::load("smarty");
	    switch ($profile_type)
		{
		    case PROFILE_INSTRUCTOR:
		        $profile = new Profile();
		        break;
		        
		    case PROFILE_SUCCESS:
		        $profile = new Success_profile();
		        break;
		        
		    default:
		        return ("Error");   
		}
		$profile_list = $profile->getAllProfileTitle();
		$smarty->assign("profile_list",$profile_list);
		$html 	= $smarty->fetch("admin/profile/profile_list.tpl");
		$div	= XOAD_HTML::getElementById('profile_list_section');
		$div->innerHTML = $html;
	}
	
	function editProfile($profile_type)
	{
	    $smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
	    switch ($profile_type)
	    {
	        case PROFILE_INSTRUCTOR:
	           $page_message = PAGE_MESSAGE_EDIT_INSTRUCTOR_PROFILE;
	           $profile_type = PROFILE_INSTRUCTOR;
	           $profile = new Profile();
	           $current_url = APPLICATION_URL . "admin/profile/" . ADMIN_TOPMENU_INSTRUCTOR . "/" . ADMIN_SUBMENU_EDIT_INSTRUCTOR_PROFILE;
	           break;
	                   
	        case PROFILE_SUCCESS:
	           $page_message = PAGE_MESSAGE_EDIT_SUCCESS_PROFILE;
	           $profile_type = PROFILE_SUCCESS;
	           $profile = new Success_profile();
	           $current_url = APPLICATION_URL . "admin/profile/" . ADMIN_TOPMENU_ALUMNI . "/" . ADMIN_SUBMENU_EDIT_SUCCESS_PROFILE;
	           break;
	        
	        default:
	            break;
	    }
	    $smarty->assign("page_message",$page_message);
	    $smarty->assign("current_profile_type",$profile_type);
	    $profileInfo = $profile->getAllProfileTitle();
	    $smarty->assign("profile_list",$profileInfo);	    
	    $smarty->assign("current_url",$current_url);
	    $content = $smarty->fetch("admin/profile/profile.tpl");
	    $smarty->assign("content",$content);
	}
	
    function profileAction()
    {
        global $arrArgs;
        $top_index = $arrArgs[0];
        $select = $arrArgs[1];
        $this->prepareLayout($top_index,$select);
        
        switch($top_index)
        {
            case ADMIN_TOPMENU_INSTRUCTOR:
                $this->editProfile(PROFILE_INSTRUCTOR);
                break;
                
            case ADMIN_TOPMENU_ALUMNI:
                $this->editProfile(PROFILE_SUCCESS);
                break;
                
            default:
                redirect($current_url);
        }
        $smarty 	= DRX::load("smarty");
        $smarty->display("admin/admin.tpl");
    }
    
    function saveprofileAction()
    {
        $return_url     = $_REQUEST['return_url'];
        $profile_id     = $_REQUEST['profile_id'];
        $profile_type   = $_REQUEST['profile_type'];
        $profile_title  = $_REQUEST['profile_title'];
        
        $profile_content= stripslashes($_REQUEST['fck_editor']);
        
        switch ($profile_type)
        {
            case PROFILE_INSTRUCTOR:
                $profile  = new Profile();
                $profile2 = new Profile();
                $profile3 = new Profile();
                $upload_dir = PROFILE_INSTRUCTOR_IMAGE_DIR;
                break;
                
            case PROFILE_SUCCESS:
                $profile  = new Success_profile();
                $profile2 = new Success_profile();
                $profile3 = new Success_profile();
                $upload_dir = PROFILE_SUCCESS_IMAGE_DIR;
                break;
                
        }
        if ($profile_id)
        {
            //edit profile
            $profile->updateProfileInfo($profile_id,$profile_title,$profile_content);
            $file_id = $profile_id;
        }
        else
        {
            $profile->addProfile($profile_title,$profile_content);
            $file_id = $profile2->getLastProfileID();
        }
        
        if ($_FILES['profile_image1']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['profile_image1']['tmp_name'];
            $filename = $_FILES['profile_image1']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = $upload_dir . "profile$file_id" . "_1." . $file_ext;
            
            if (!move_uploaded_file($_FILES['profile_image1']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $profile_file[] = "";
            }
            else
            {
                $profile_file[]	= $server_copy;                
                chmod("$server_copy",0777);
                $image_resizer = new IO_Image();
                $imageArg['thumb_width']    = DEFAULT_IMAGE_THUMB_WIDTH;
                $imageArg['filepath']       = $server_copy;
                $image_resizer->MakeThumbImage($imageArg);
            }
  		}
  		else 
  		{
  		    $profile_file[] = "";
  		}
  		
  		if ($_FILES['profile_image2']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['profile_image2']['tmp_name'];
            $filename = $_FILES['profile_image2']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = $upload_dir . "profile$file_id" . "_2." . $file_ext;
            
            if (!move_uploaded_file($_FILES['profile_image2']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $profile_file[] = "";
            }
            else
            {
                $profile_file[]	= $server_copy;
                chmod("$server_copy",0777);
            }
  		}
  		else 
  		{
  		    $profile_file[] = "";
  		} 		
  		
  		if ($_FILES['profile_image3']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['profile_image3']['tmp_name'];
            $filename = $_FILES['profile_image3']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = $upload_dir . "profile$file_id" . "_3." . $file_ext;
            
            if (!move_uploaded_file($_FILES['profile_image3']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $profile_file[] = "";
            }
            else
            {
                $profile_file[]	= $server_copy;
                chmod("$server_copy",0777);
            }
  		}
  		else 
  		{
  		    $profile_file[] = "";
  		}
//  		var_dump($profile_file);die;
  		
  		$profile3->updateProfileImage($file_id,$profile_file);
        
        if (isset($result))
        {
            echo $result;
        }
        else 
        {
            redirect($return_url);
        }
    }
	
    function getNewsIconURL($image_location)
    {
        $list = explode("/",$image_location);
	    $filename = $list[count($list)-1];
	    $url = NEWS_IMAGE_URL . "$filename";
	    return $url;
    }
    
	function editNews($args)
	{
	    $news_id 	= $args["news_id"];
		
		$news 		= new News();
		$newsInfo	= $news->getNewsInfoByID($news_id);
		$newsInfo->fetch();
		$news_title	= $newsInfo->news_title;
		$news_subtitle = $newsInfo->news_subtitle;
		$news_time  = $newsInfo->news_time;
		$news_modified = $newsInfo->news_modified;
		$news_order   = $newsInfo->news_order;
		$fck_value  = $newsInfo->news_content;
		$news_icon = $newsInfo->news_icon;
		$smarty 	= DRX::load("smarty");
		if ($news_icon)
		{
		    $news_image = $this->getNewsIconURL($news_icon);
		    $smarty->assign("news_image",$news_image);
    		$current_news_icon = $smarty->fetch("admin/news/news_icon.tpl");
    		$smarty->assign("current_news_icon",$current_news_icon);
		}
		$fck		= $this->createFCK($fck_value);
		$smarty->assign("page_message","Edit news:");
		$smarty->assign("news_id",$news_id);
		$smarty->assign("news_title",$news_title);
		$smarty->assign("news_subtitle",$news_subtitle);
		$smarty->assign("news_time",$news_time);
		$smarty->assign("news_modified",$news_modified);
		$smarty->assign("news_order",$news_order);
		
		$smarty->assign("fck_editor",$fck);
		
		$content = $smarty->fetch("admin/news/edit_news.tpl");
		
		$div = XOAD_HTML::getElementById('edit_news_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function savenewsAction()
	{
		$return_url = $_REQUEST['return_url'];
	    $news_id = $_REQUEST['news_id'];
		$news_title = $_REQUEST['news_title'];
		$news_subtitle = $_REQUEST['news_subtitle'];
		$news_order = $_REQUEST['news_order'];
		$news_hide = $_REQUEST['news_hide'];
		$news_content = stripslashes($_REQUEST['fck_editor']);
		
		if ($news_id)
		{
		    $file_id = $news_id;
		}
		else
		{
		    $news = new News();
		    $file_id = $news->getNextNewsID();
		}
		
		if ($_FILES['news_icon']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['news_icon']['tmp_name'];
            $filename = $_FILES['news_icon']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = NEWS_IMAGE_DIR . "news$file_id." . $file_ext;
            
            if (!move_uploaded_file($_FILES['news_icon']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $news_icon = "";
            }
            else
            {
                $news_icon = $server_copy;
                chmod("$server_copy",0777);
            }
  		}
  		else 
  		{
  		    $news_icon = "";
  		}
  		//Replace by a thumbnail image
  		if ($news_icon)
  		{
  		    $imageArgs['filepath']      = $news_icon;
  		    $imageArgs['thumb_width']   = DEFAULT_NEWS_THUMB_WIDTH;
  		    $io_image = new IO_Image();
  		    $result = $io_image->MakeThumbImage($imageArgs);
  		    if ($result=="OK")
  		    {
  		        $new_name = dirname($news_icon) . "/thumb_" . basename($news_icon);
  		        @unlink($news_icon);
  		        $news_icon = $new_name;
  		    }
  		}
  		
		$news = new News();
	    if ($news_id)
		{
		    //update request
    		$news->updateNews($news_id,$news_title,$news_subtitle,$news_content,$news_hide,$news_order,$news_icon);
		}
		else 
		{
		    //add request
    		$news->addNews($news_title,$news_subtitle,$news_content,$news_hide,$news_order,$news_icon);
		}
		redirect($return_url);
	}
	
	function listNews()
	{
		$smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
		$current_url = APPLICATION_URL . ADMIN_URL . ADMIN_ACTION_NEWS . ADMIN_TOPMENU_NEWS . "/" . ADMIN_SUBMENU_NEWS_EDIT_NEWS_INDEX;

		$smarty->assign("current_url",$current_url);
		$news = new News();
		$news_list = $news->getAllNewsInfo();

		$smarty->assign("news_list",$news_list);
		$content = $smarty->fetch("admin/news/news.tpl");
		$smarty->assign("content",$content);
	}
	
	function addNews()
	{
		$smarty 	= DRX::load("smarty");
	    $return_url = APPLICATION_URL . ADMIN_URL . ADMIN_ACTION_NEWS . ADMIN_TOPMENU_NEWS . "/1";
		$news     = new News();
		$news_order = $news->getNextNewsOrder();
		$fck		= $this->createFCK();
		
		$smarty->assign("page_message","Add news:");
		$smarty->assign("fck_editor",$fck);
		$smarty->assign("return_url",$return_url);
		$smarty->assign("news_order",$news_order);
		
		$content = $smarty->fetch("admin/news/edit_news.tpl");
		
		$div = XOAD_HTML::getElementById('edit_news_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function deleteNews($args)
	{
	    $news_id = $args['news_id'];
		$news = new News();
		$result = $news->deleteNews($news_id);
		$this->refreshNewsList();
		return "OK";
	}
	
	function toggleNewsPublish($args)
	{
	    $news_id = $args['news_id'];
	    $publish = $args['publish_value'];
	    $news  = new News();
	    $news->tooglePublish($news_id,$publish);
	    $this->refreshNewsList();
	    return "OK";
	}
	
	function refreshNewsList()
	{
	    $smarty 	= DRX::load("smarty");
		$news = new News();
		$news_list = $news->getAllNewsInfo();
		$smarty->assign("news_list",$news_list);
		$html 	= $smarty->fetch("admin/news/news_list.tpl");
		$div	= XOAD_HTML::getElementById('news_list_section');
		$div->innerHTML = $html;
	}
	
	function newsAction()
	{	    
		global $arrArgs;
		$top_index = $arrArgs[0];
		$select = $arrArgs[1];
		$this->prepareLayout($top_index,$select);
        $this->listNews();
		$smarty 	= DRX::load("smarty");
		$smarty->display("admin/admin.tpl");
	}
	
	function addFaq()
	{
	    $smarty       = DRX::load("smarty");
	    $content = $smarty->fetch("admin/faq/edit_faq.tpl");
		$div = XOAD_HTML::getElementById('edit_faq_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function saveFaq($args)
	{
	    $form = $args['form'];
	    $faq  = new Faq();
	    if ($form['faq_id'])
	    {
            $faq->updateFaq($form);
	    }
	    else 
	    {
	        $faq->addFaq($form);
	    }
	    $this->refreshFaqList();
	    return "OK";
	}
	
	function editFaq($args)
	{
	    $faq_id 	= $args["faq_id"];
		$faq          = new Faq();
		$faqInfo      = $faq->getFaqInfoByID($faq_id);
		$faqInfo->fetch();
		$faq_id       = $faqInfo->faq_id;
		$faq_question = $faqInfo->faq_question;
		$faq_answer   = $faqInfo->faq_answer;
		$faq_order    = $faqInfo->faq_order;
		$smarty       = DRX::load("smarty");
		
		$smarty->assign("faq_id",$faq_id);
		$smarty->assign("faq_question",$faq_question);
		$smarty->assign("faq_answer",$faq_answer);
		$smarty->assign("faq_order",$faq_order);
		
		$content = $smarty->fetch("admin/faq/edit_faq.tpl");
		
		$div = XOAD_HTML::getElementById('edit_faq_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function deleteFaq($args)
	{
	    $faq_id = $args['faq_id'];
	    $faq     = new Faq();
	    $faq->deleteFaq($faq_id);
	    $this->refreshFaqList();
	    return "OK";
	}	
	
	function refreshFaqList()
	{
	    $smarty 	= DRX::load("smarty");
		$faq = new Faq();
		$faq_list = $faq->getAllFaqInfo();
		$smarty->assign("faq_list",$faq_list);
		$html 	= $smarty->fetch("admin/faq/faq_list.tpl");
		$div	= XOAD_HTML::getElementById('faq_list_section');
		$div->innerHTML = $html;
	}
	
	function editFaqGroup()
	{
	    $smarty = DRX::load('smarty');
	    $group = new Faqgroup();
		$group_list = $group->getAllFaqGroupInfo();
		$smarty->assign("group_list",$group_list);
		$content  = $smarty->fetch("admin/faq/group.tpl");
		$smarty->assign("content",$content);
	}
	
	function toggleGroupHide($args)
	{
	    $group_id = $args['group_id'];
	    $publish = $args['hide_value'];
	    $group    = new Faqgroup();
	    $group->toogleHide($group_id,$publish);
	    $this->refreshGroupList();
	    return "OK";
	}
	
	function saveGroup($args)
	{
	    $form = $args['form'];
	    $group = new Faqgroup();
	    if ($form['group_id'])
	    {
	        $group->updateGroup($form);
	    }
	    else 
	    {
	        $group->addgroup($form);
	    }
	    $this->refreshGroupList();
	    return "OK";
	}
	
	function refreshGroupList()
	{
	    $smarty 	= DRX::load("smarty");
		$group = new Faqgroup();
		$group_list = $group->getAllFaqGroupInfo();
		$smarty->assign("group_list",$group_list);
		$html 	= $smarty->fetch("admin/faq/group_list.tpl");
		$div	= XOAD_HTML::getElementById('group_list_section');
		$div->innerHTML = $html;
	}
	
	function editGroup($args)
	{
	    $smarty 	= DRX::load("smarty");
	    $group_id = $args['group_id'];
	    $group    = new Faqgroup();
		$groupInfo = $group->getFaqGroupInfoByID($group_id);
		$groupInfo->fetch();
		$group_name = $groupInfo->group_name;
		$group_order = $groupInfo->group_order;
		$smarty->assign("group_id",$group_id);
		$smarty->assign("group_name",$group_name);
		$smarty->assign("group_order",$group_order);
		$smarty->assign("page_message","Edit group");
	    $html 	= $smarty->fetch("admin/faq/edit_group.tpl");
		$div	= XOAD_HTML::getElementById('edit_group_section');
		$div->innerHTML = $html;
		return "OK";
	}
	
	function deleteGroup($args)
	{
	    $group_id = $args['group_id'];
	    
	    $group = new Group_faq();
	    $question = $group->getAllQuestionInGroup($group_id);
	    if ($question->fetch())
	    {
	        return "You cannot delete this group because it is not empty";
	    }
	    else 
	    {
    	    $group = new Faqgroup();
    	    $group->deletegroup($group_id);
    	    $this->refreshGroupList();
    	    return "OK";
	    }
	}
	
	function deleteGroupQuestion($args)
	{
	    $faq_id = $args['faq_id'];
	    $group_id = $args['group_id'];
	    $group_faq  = new Group_faq();
	    $group_faq->deleteQuestion($group_id,$faq_id);
	    $this->refreshGroupQuestionList($group_id);
	    return "OK";
	}
	
	function editGroupQuestion($args)
	{
	    $smarty 	= DRX::load("smarty");
	    $group_id   = $args['group_id'];
	    
	    $group = new Faqgroup();
	    $groupInfo = $group->getFaqGroupInfoByID($group_id);
	    $groupInfo->fetch();
	    $group_name = $groupInfo->group_name;
	    $smarty->assign("group_id",$group_id);
	    $smarty->assign("group_name",$group_name);
	    
	    $group_faq  = new Group_faq();
	    $question_list = $group_faq->getAllQuestionInGroup($group_id);
	    $smarty->assign("question_list",$question_list);
	    $group_question = $smarty->fetch("admin/faq/group_question_list.tpl");
	    $smarty->assign("group_question",$group_question);
	    
	    $group_faq = new Group_faq();
	    $other_question_list = $group_faq->getOtherQuestion($group_id);
	    $smarty->assign("other_question_list",$other_question_list);
	    $other_question = $smarty->fetch("admin/faq/other_question.tpl");
	    $smarty->assign("other_question",$other_question);
	    
	    $html = $smarty->fetch("admin/faq/edit_group_question.tpl");
	    $div	= XOAD_HTML::getElementById('group_question_section');
		$div->innerHTML = $html;
		return "OK";
	}
	
	function saveGroupQuestionOrder($args)
	{
	    $smarty 	= DRX::load("smarty");
	    $group_id   = $args['group_id'];
	    $faq_id     = $args['faq_id'];
	    $group_order = $args['question_order'];
	    
	    $group = new Group_faq();
	    $group->updateQuestionOrder($group_id,$faq_id,$group_order);
	    
	    $group_faq  = new Group_faq();
	    $question_list = $group_faq->getAllQuestionInGroup($group_id);
	    $smarty->assign("question_list",$question_list);
	    $group_question = $smarty->fetch("admin/faq/group_question_list.tpl");
	    
	    $div	= XOAD_HTML::getElementById('group_question_list');
		$div->innerHTML = $group_question;
		return "OK";
	}
	
	function addQuestionToGroup($args)
	{
	    $faq_id = $args['faq_id'];
	    $group_id = $args['group_id'];
	    $order = $args['faq_order'];
	    $group_faq  = new Group_faq();
	    $group_faq->addQuestion($group_id,$faq_id,$order);
	    $this->refreshGroupQuestionList($group_id);
	    return "OK";
	}
	
	function refreshGroupQuestionList($group_id)
	{
	    $smarty 	= DRX::load("smarty");
	    $group_faq  = new Group_faq();
	    $question_list = $group_faq->getAllQuestionInGroup($group_id);
	    $smarty->assign("question_list",$question_list);
	    $group_question = $smarty->fetch("admin/faq/group_question_list.tpl");
	    
	    $div	= XOAD_HTML::getElementById('group_question_list');
		$div->innerHTML = $group_question;
	    
	    $group_faq = new Group_faq();
	    $other_question_list = $group_faq->getOtherQuestion($group_id);
	    $smarty->assign("other_question_list",$other_question_list);
	    $other_question = $smarty->fetch("admin/faq/other_question.tpl");
	    
	    $div	= XOAD_HTML::getElementById('group_other_question');
		$div->innerHTML = $other_question;
		return "OK";
	}
	
	function addGroup()
	{
	    $smarty 	= DRX::load("smarty");
	    $smarty->assign("page_message","Add group");
	    $html 	= $smarty->fetch("admin/faq/edit_group.tpl");
		$div	= XOAD_HTML::getElementById('edit_group_section');
		$div->innerHTML = $html;
		return "OK";
	}
	
	function listFaq()
	{
	    $smarty = DRX::load('smarty');
	    $faq = new Faq();
		$faq_list = $faq->getAllFaqInfo();
		$smarty->assign("faq_list",$faq_list);
		$question = new Question();
		$question_list = $question->getAllQuestionInfo();
		$smarty->assign("question_list",$question_list);
		$content  = $smarty->fetch("admin/faq/faq.tpl");
		$smarty->assign("content",$content);
	}
	
	function faqAction()
	{
	    global $arrArgs;
		$top_index = $arrArgs[0];
		$select = $arrArgs[1];
		$smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
		$this->prepareLayout($top_index,$select);
		
		switch ($select)
		{
		    case ADMIN_SUBMENU_FAQ_EDIT_FAQ_INDEX:
		        $this->listFaq();
		        break;
		    
		    case ADMIN_SUBMENU_FAQ_EDIT_GROUP_INDEX:
		        $this->editFaqGroup();
		        break;
	        
		    default:
		        redirect(APPLICATION_URL . ADMIN_URL);
		}		
		$smarty->display("admin/admin.tpl");
	}
	
	function answerQuestion($args)
	{
	    $question_id = $args['question_id'];
	    
	    $question          = new Question();
		$questionInfo      = $question->getQuestionInfoByID($question_id);
		$questionInfo->fetch();
		$question_id       = $questionInfo->question_id;
		$question_detail = $questionInfo->question_detail;
		$question_answer   = $questionInfo->question_answer;
		$smarty       = DRX::load("smarty");
		
		$smarty->assign("question_id",$question_id);
		$smarty->assign("question_detail",$question_detail);
		$smarty->assign("question_answer",$question_answer);
		
		$content = $smarty->fetch("admin/faq/answer_question.tpl");
		
		$div = XOAD_HTML::getElementById('edit_question_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function saveQuestion($args)
	{
	    $form = $args['form'];
	    $question  = new Question();
        $question->updateQuestion($form);
	    $this->refreshQuestionList();
	    return "OK";
	}
	
	function refreshQuestionList()
	{
	    $question = new Question();
		$question_list = $question->getAllQuestionInfo();
		$smarty       = DRX::load("smarty");
		$smarty->assign("question_list",$question_list);
		$content  = $smarty->fetch("admin/faq/question_list.tpl");
		$div = XOAD_HTML::getElementById('question_list_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function deleteQuestion($args)
	{
	    $question_id = $args['question_id'];
	    $question     = new Question();
	    $question->deleteQuestion($question_id);
	    $this->refreshQuestionList();
	    return "OK";
	}
	
	function copyQuestion($args)
	{
	    $question_id = $args['question_id'];
	    $faq         = new Faq();
	    $faq->copyQuestionToFaq($question_id);
	    $this->refreshFaqList();
	    return "OK";
	}
	
	function editLink($args)
	{
	    $link_id = $args['link_id'];
        $link    = new Link();
		$linkInfo= $link->getLinkInfoByID($link_id);
		$linkInfo->fetch();
		$link_name     = $linkInfo->link_name;
		$link_title    = $linkInfo->link_title;
		$link_url      = $linkInfo->link_url;
		$smarty       = DRX::load("smarty");
		
		$smarty->assign("link_id",$link_id);
		$smarty->assign("link_name",$link_name);
		$smarty->assign("link_title",$link_title);
		$smarty->assign("link_url",$link_url);
		
		$content = $smarty->fetch("admin/link/edit_link.tpl");
		
		$div = XOAD_HTML::getElementById('edit_link_section');
		$div->innerHTML = $content;
		return "OK";
	}
	
	function saveLink($args)
	{
	    $form = $args['form'];
	    $link_id  = $form['link_id'];
	    $link_title  = $form['link_title'];
	    $link_url = $form['link_url'];
	    $link = new Link();
	    $link->updateLink($link_id,$link_title,$link_url);
	    $this->refreshLinkList();
	    return "OK";
	}
	
	function refreshLinkList($args)
	{
	    $smarty 	= DRX::load("smarty");
		$link = new Link();
		$link_list = $link->getAllLinkInfo();
		$smarty->assign("link_list",$link_list);
		$html 	= $smarty->fetch("admin/link/link_list.tpl");
		$div	= XOAD_HTML::getElementById('link_list_section');
		$div->innerHTML = $html;
	}
	
	function linkAction()
	{
	    global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];
		$smarty 	= DRX::load("smarty");	
		//ajax config
		if (XOAD_Server::runServer()) {
  			exit; //inspect this
  		}
  		//to include javascript files written by XOAD for AJAX usage
  		$smarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL."library/external/XOAD"));
  		//register controller in js 		
  		$smarty->assign("xoad_handler", XOAD_CLient::register($this));
  		
		$this->prepareLayout($top_index,$sub_index);
		
		$link = new Link();
		$link_list = $link->getAllLinkInfo();
		$smarty->assign("link_list",$link_list);
		$content  = $smarty->fetch("admin/link/link.tpl");
		$smarty->assign("content",$content);
		$smarty->display("admin/admin.tpl");
	}
	
	function savebannerAction()
	{
	    $menu_id = $_REQUEST['menu_id'];
	    $return_url   = $_REQUEST['return_url'];
		if ($_FILES['menu_banner']['error'] == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES['menu_banner']['tmp_name'];
            $filename = $_FILES['menu_banner']['name'];
            $getExt = explode ('.', $filename);
    	    $file_ext = $getExt[count($getExt)-1];
            $server_copy = TOPMENU_BANNER_DIR . "top$menu_id." . $file_ext;
            
            if (!move_uploaded_file($_FILES['menu_banner']['tmp_name'],$server_copy))
            {
                $result = "Error while uploading file";
                $menu_banner = "";
            }
            else
            {
                $menu_banner = $server_copy;
                chmod("$server_copy",0777);
            }
  		}
	    $topmenu = new Topmenu();
	    $topmenu->updateBanner($menu_id,$menu_banner);
	    redirect($return_url);
	}
	
	function bannerAction()
	{
	    global $arrArgs;
		$top_index = $arrArgs[0];
		$sub_index = $arrArgs[1];
		$smarty 	= DRX::load("smarty");	
		$this->prepareLayout($top_index,$sub_index);
		
		switch ($top_index)
		{
		    case ADMIN_TOPMENU_INSTRUCTOR:
		        $menu_id = FACULTY_ID;
		        break;
		    
		    case ADMIN_TOPMENU_FAQ:
		        $menu_id = FAQ_ID;
		        break;
		        
		    case ADMIN_TOPMENU_CONTACT:
		        $menu_id = CONTACT_ID;
		        break;
		    
		    case ADMIN_TOPMENU_NEWS:
		        $menu_id = NEWS_ID;
		        break;
		        
	        default:
	            redirect(APPLICATION_URL . ADMIN_URL);
		}
		
		$topmenu   = new Topmenu();
		$topmenu->prepareTopBanner($menu_id);
		$smarty->assign("menu_id",$menu_id);
		$smarty->assign("return_url",APPLICATION_URL . ADMIN_URL . ADMIN_ACTION_BANNER . "$top_index/$sub_index");
		$content = $smarty->fetch("admin/banner/banner.tpl");
		$smarty->assign("content",$content);
		$smarty->display("admin/admin.tpl");
	}
}
?>